from django.shortcuts import render,redirect
from django.http import HttpResponse,Http404
from .models import Question,Choice
# Create your views here.

def indexPage(request):
    Q_list = Question.objects.all()
    return render(request,'polls/index.html',{'q_list':Q_list})

def quesPage(request):
    q_id = request.GET['q_id']
    request.session['q_id'] = q_id
    c_list = Choice.objects.filter(question_id=q_id)
    return render(request,'polls/ques.html',{'c_list':c_list})

def countVotes(request):
    q_id = request.session.get('q_id')
    chioce_id = request.GET['choice']
    dq_chioce = Choice.objects.get(pk=chioce_id)
    dq_chioce.votes = dq_chioce.votes+1
    dq_chioce.save()
    c_list = Choice.objects.filter(question_id=q_id)

    return render(request,'polls/votes.html',{'c_list':c_list})